GreenCoding

# The new frontier for software development

Download our thought

Resource compression

Resource compression is the process of reducing the size of digital resources, such as images, CSS files, JavaScript files, and other assets used in web development, without compromising their quality or functionality. The main goal of resource compression is to minimize the data transferred over the internet, leading to faster loading times and reduced bandwidth consumption.

Here's an example of how you can apply resource compression techniques in HTML, CSS, and JavaScript to make your website more energy-efficient:

1.Image Compression:Images can be compressed to reduce their file size while maintaining acceptable quality. Common image compression formats include JPEG, WebP, and PNG. By using compressed images, web pages load faster and consume less data.

Image Compression (using WebP format):
                
                    <!-- Original Image -->
                    <img src="original-image.jpg" alt="Original Image">
                    
                    <!-- Compressed Image in WebP format -->
                    <picture>
                      <source srcset="compressed-image.webp" type="image/webp">
                      <img src="original-image.jpg" alt="Compressed Image">
                    </picture>
                    
                
              

2.CSS and JavaScript Minification:CSS and JavaScript files often contain unnecessary whitespace, comments, and long variable names. Minification removes these extra characters and reduces the file size, leading to faster loading times.

Original CSS File (styles.css):
               
                   body {
                       background-color: #f0f0f0;
                       color: #333;
                     }
                     
               
             
Minified CSS:
               
                   body{background-color:#f0f0f0;color:#333}
               
             
Original JavaScript File (script.js):
               
                   function greet(name) {
                       console.log("Hello, " + name + "!");
                     }      
               
             
Minified JavaScript:
               
                   function greet(e){console.log("Hello, "+e+"!")}
       
               
             

3.Gzip and Brotli Compression:Servers can compress text-based resources (such as HTML, CSS, and JavaScript) using Gzip or Brotli algorithms before sending them to the client's browser. This compression reduces the data transferred and improves overall performance.

4.Lazy Loading:Media elements like images and videos can be loaded only when they come into the user's viewport, saving bandwidth on initial page load.

Lazy Loading (Image):
            
                <!-- Original Image (Lazy Loading) -->
                <img src="placeholder.jpg" data-src="actual-image.jpg" alt="Lazy Loaded Image">
            
          

In this example, the data-src attribute holds the actual image URL, while the src attribute contains a placeholder image. JavaScript can be used to load the actual image when it comes into the user's viewport.

By applying resource compression techniques, web developers can create more efficient and sustainable websites that load faster, consume less energy, and have a reduced carbon footprint, contributing to the principles of green coding and a greener digital environment.